package com.multidisplay.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


@Controller //注解，组件扫描加入到spring容器中
@RequestMapping("/file") //类controller的映射路径
public class FileController {
    @RequestMapping("/download")
    public void download(HttpServletRequest request,HttpServletResponse response) {
        String filePath="F:\\1.jpg";
        try {
            InputStream bis= new BufferedInputStream(new FileInputStream(new File(filePath)));
            String fileName="张三.jpg";
            fileName=URLEncoder.encode(fileName,"UTF-8");
            response.addHeader("Content-Disposition", "attachment;filename="+fileName);
            response.setContentType("multipart/form-data");
            BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
            int len=0;
            while((len=bis.read())!=-1) {
                out.write(len);
                out.flush();
            }
            out.close();
            bis.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
